﻿/******************************************************************************
文件名称    :   MySqlSurgeryDataProvider.cs
创建日期    :   2011/5/13 6:18:36
功能描述    :   Surgery实体数据提供程序MySql实现类
修改日志    :   
    2011-12-15 06:18  马利
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Dare.Utilities.Data;
using Stone.Data.Providers;
using Stone.Components.Entities;

namespace Stone.Data.MySql.Providers
{
    /// <summary>
    /// Surgery实体数据提供程序MySql实现类
    /// </summary>
    public class MySqlSurgeryDataProvider : SurgeryDataProvider
    {
        public MySqlSurgeryDataProvider()
            : base()
        {
        }

        public override void Insert(TransactionManager manager, Surgery entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_surgery(surgery_date, kidney_id, softTube_id, hardTube_id, sperm_id, pg_id, kidneyPos, stone_size, stone_position, recordFile_id, infection_id, stone_remain, comment) values(?surgery_date, ?kidney_id, ?softTube_id, ?hardTube_id, ?sperm_id, ?pg_id, ?kidneyPos, ?stone_size, ?stone_position, ?recordFile_id, ?infection_id, ?stone_remain, ?comment);" +
                    "select id, time_stamp from tb_surgery where id = last_insert_id();";
                GenericDbHelper.AddParameter(mgr, cmd, "?surgery_date", DbType.Date).Value = entity.SurgeryDate;
                GenericDbHelper.AddParameter(mgr, cmd, "?kidney_id", DbType.Int64).Value = entity.KidneyId == null ? DBNull.Value : (object)entity.KidneyId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?softTube_id", DbType.Int64).Value = entity.SoftTubeId == null ? DBNull.Value : (object)entity.SoftTubeId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?hardTube_id", DbType.Int64).Value = entity.HardTubeId == null ? DBNull.Value : (object)entity.HardTubeId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?sperm_id", DbType.Int64).Value = entity.SpermId == null ? DBNull.Value : (object)entity.SpermId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?pg_id", DbType.Int64).Value = entity.PgId == null ? DBNull.Value : (object)entity.PgId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?kidneyPos", DbType.Int32).Value = entity.KidneyPos == null ? DBNull.Value : (object)entity.KidneyPos.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?stone_size", DbType.Decimal).Value = entity.StoneSize == null ? DBNull.Value : (object)entity.StoneSize.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?stone_position", DbType.String, 200).Value = entity.StonePosition == null ? DBNull.Value : (object)entity.StonePosition;
                GenericDbHelper.AddParameter(mgr, cmd, "?recordFile_id", DbType.Int64).Value = entity.RecordFileId == null ? DBNull.Value : (object)entity.RecordFileId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?infection_id", DbType.Int64).Value = entity.InfectionId == null ? DBNull.Value : (object)entity.InfectionId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?stone_remain", DbType.String, 200).Value = entity.StoneRemain == null ? DBNull.Value : (object)entity.StoneRemain;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;

                //执行数据库操作,并获得主键和更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.Id = reader.GetInt32(0);
                        entity.TimeStamp = reader.GetDateTime(1);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //插入关联
                InsertRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Import(TransactionManager manager, IEnumerable<Surgery> entities)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_surgery(id, surgery_date, kidney_id, softTube_id, hardTube_id, sperm_id, pg_id, kidneyPos, stone_size, stone_position, recordFile_id, infection_id, stone_remain, comment, time_stamp) values(?id, ?surgery_date, ?kidney_id, ?softTube_id, ?hardTube_id, ?sperm_id, ?pg_id, ?kidneyPos, ?stone_size, ?stone_position, ?recordFile_id, ?infection_id, ?stone_remain, ?comment, ?time_stamp);";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?surgery_date", DbType.Date);
                GenericDbHelper.AddParameter(mgr, cmd, "?kidney_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?softTube_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?hardTube_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?sperm_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?pg_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?kidneyPos", DbType.Int32);
                GenericDbHelper.AddParameter(mgr, cmd, "?stone_size", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?stone_position", DbType.String, 200);
                GenericDbHelper.AddParameter(mgr, cmd, "?recordFile_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?infection_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?stone_remain", DbType.String, 200);
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String);
                GenericDbHelper.AddParameter(mgr, cmd, "?time_stamp", DbType.DateTime);

                //执行数据库操作
                foreach (Surgery entity in entities)
                {
                    int i = 0;
                    cmd.Parameters[i++].Value = entity.Id;
                    cmd.Parameters[i++].Value = entity.SurgeryDate;
                    cmd.Parameters[i++].Value = entity.KidneyId == null ? DBNull.Value : (object)entity.KidneyId.Value;
                    cmd.Parameters[i++].Value = entity.SoftTubeId == null ? DBNull.Value : (object)entity.SoftTubeId.Value;
                    cmd.Parameters[i++].Value = entity.HardTubeId == null ? DBNull.Value : (object)entity.HardTubeId.Value;
                    cmd.Parameters[i++].Value = entity.SpermId == null ? DBNull.Value : (object)entity.SpermId.Value;
                    cmd.Parameters[i++].Value = entity.PgId == null ? DBNull.Value : (object)entity.PgId.Value;
                    cmd.Parameters[i++].Value = entity.KidneyPos == null ? DBNull.Value : (object)entity.KidneyPos.Value;
                    cmd.Parameters[i++].Value = entity.StoneSize == null ? DBNull.Value : (object)entity.StoneSize.Value;
                    cmd.Parameters[i++].Value = entity.StonePosition == null ? DBNull.Value : (object)entity.StonePosition;
                    cmd.Parameters[i++].Value = entity.RecordFileId == null ? DBNull.Value : (object)entity.RecordFileId.Value;
                    cmd.Parameters[i++].Value = entity.InfectionId == null ? DBNull.Value : (object)entity.InfectionId.Value;
                    cmd.Parameters[i++].Value = entity.StoneRemain == null ? DBNull.Value : (object)entity.StoneRemain;
                    cmd.Parameters[i++].Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                    cmd.Parameters[i++].Value = entity.TimeStamp;
                    cmd.ExecuteNonQuery();
                }

                //插入关联
                InsertRelations(mgr, cmd, entities);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Update(TransactionManager manager, Surgery entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //更新SQL语句
                cmd.CommandText = "update tb_surgery set surgery_date = ?surgery_date, kidney_id = ?kidney_id, softTube_id = ?softTube_id, hardTube_id = ?hardTube_id, sperm_id = ?sperm_id, pg_id = ?pg_id, kidneyPos = ?kidneyPos, stone_size = ?stone_size, stone_position = ?stone_position, recordFile_id = ?recordFile_id, infection_id = ?infection_id, stone_remain = ?stone_remain, comment = ?comment where id = ?id;"
                        + "select time_stamp from tb_surgery where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?surgery_date", DbType.Date).Value = entity.SurgeryDate;
                GenericDbHelper.AddParameter(mgr, cmd, "?kidney_id", DbType.Int64).Value = entity.KidneyId == null ? DBNull.Value : (object)entity.KidneyId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?softTube_id", DbType.Int64).Value = entity.SoftTubeId == null ? DBNull.Value : (object)entity.SoftTubeId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?hardTube_id", DbType.Int64).Value = entity.HardTubeId == null ? DBNull.Value : (object)entity.HardTubeId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?sperm_id", DbType.Int64).Value = entity.SpermId == null ? DBNull.Value : (object)entity.SpermId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?pg_id", DbType.Int64).Value = entity.PgId == null ? DBNull.Value : (object)entity.PgId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?kidneyPos", DbType.Int32).Value = entity.KidneyPos == null ? DBNull.Value : (object)entity.KidneyPos.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?stone_size", DbType.Decimal).Value = entity.StoneSize == null ? DBNull.Value : (object)entity.StoneSize.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?stone_position", DbType.String, 200).Value = entity.StonePosition == null ? DBNull.Value : (object)entity.StonePosition;
                GenericDbHelper.AddParameter(mgr, cmd, "?recordFile_id", DbType.Int64).Value = entity.RecordFileId == null ? DBNull.Value : (object)entity.RecordFileId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?infection_id", DbType.Int64).Value = entity.InfectionId == null ? DBNull.Value : (object)entity.InfectionId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?stone_remain", DbType.String, 200).Value = entity.StoneRemain == null ? DBNull.Value : (object)entity.StoneRemain;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = entity.Id;

                //执行数据库操作,并获得更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.TimeStamp = reader.GetDateTime(0);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //更新关联
                UpdateRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Delete(TransactionManager manager, long key)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, key);

                //删除SQL语句
                cmd.CommandText = "delete from tb_surgery where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行
                cmd.ExecuteNonQuery();
                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void DeleteAll(TransactionManager manager, string whereClause)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------具体SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, whereClause);

                //删除SQL语句
                cmd.CommandText = String.Format("delete from tb_surgery {0}",
                     ProcessWhereClause(whereClause));

                //执行
                cmd.ExecuteNonQuery();

                /*------------------------------具体SQL操作结束----------------------------------*/
            });
        }

        public override Surgery Get(TransactionManager manager, long key)
        {
            Surgery entity = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = "select * from tb_surgery where id = ?id";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行获取实体
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity = Fill(reader, null);
                    }
                }

                //获取关联
                if (entity != null)
                {
                    RetrieveRelations(mgr, cmd, entity);
                    entity.EntityState = Stone.Components.EntityState.Saved;
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return entity;
        }

        public override List<Surgery> GetPaged(TransactionManager manager, string whereClause, string orderBy, int pageIndex, int pageSize)
        {
            List<Surgery> list = new List<Surgery>(pageSize > 0 ? Math.Min(pageSize, 256) : 32);
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select * from tb_surgery {0} {1} {2}",
                    ProcessWhereClause(whereClause),
                    ProcessOrderBy(orderBy),
                    ProcessPaging(pageIndex, pageSize));

                //执行获取实体集
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(Fill(reader, null));
                    }
                }

                //获取关联
                RetrieveRelations(mgr, cmd, list);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return list;
        }

        public override int GetCount(TransactionManager manager, string whereClause)
        {
            int count = 0;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*) from tb_surgery {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                count = Convert.ToInt32(cmd.ExecuteScalar());

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return count;
        }

        public override long GetVersion(TransactionManager manager, string whereClause)
        {
            long count = 0;
            DateTime? timeStamp = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*), max(time_stamp) from tb_surgery {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        count = reader.GetInt64(0);
                        timeStamp = reader.IsDBNull(1) ? (DateTime?)null : reader.GetDateTime(1);
                    }
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return (timeStamp ?? DateTime.MinValue).Ticks ^ count;
        }

        public override Surgery Fill(IDataReader reader, Surgery entity)
        {
            if (entity == null) entity = new Surgery();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                switch (reader.GetName(i))
                {
                    case "id":
                        entity.Id = reader.GetInt64(i);
                        break;
                    case "surgery_date":
                        entity.SurgeryDate = reader.GetDateTime(i);
                        break;
                    case "kidney_id":
                        entity.KidneyId = reader[i] == DBNull.Value ? entity.KidneyId : reader.GetInt64(i);
                        break;
                    case "softTube_id":
                        entity.SoftTubeId = reader[i] == DBNull.Value ? entity.SoftTubeId : reader.GetInt64(i);
                        break;
                    case "hardTube_id":
                        entity.HardTubeId = reader[i] == DBNull.Value ? entity.HardTubeId : reader.GetInt64(i);
                        break;
                    case "sperm_id":
                        entity.SpermId = reader[i] == DBNull.Value ? entity.SpermId : reader.GetInt64(i);
                        break;
                    case "pg_id":
                        entity.PgId = reader[i] == DBNull.Value ? entity.PgId : reader.GetInt64(i);
                        break;
                    case "kidneyPos":
                        entity.KidneyPos = reader[i] == DBNull.Value ? entity.KidneyPos : reader.GetInt32(i);
                        break;
                    case "stone_size":
                        entity.StoneSize = reader[i] == DBNull.Value ? entity.StoneSize : reader.GetDecimal(i);
                        break;
                    case "stone_position":
                        entity.StonePosition = reader[i] == DBNull.Value ? entity.StonePosition : reader.GetString(i);
                        break;
                    case "recordFile_id":
                        entity.RecordFileId = reader[i] == DBNull.Value ? entity.RecordFileId : reader.GetInt64(i);
                        break;
                    case "infection_id":
                        entity.InfectionId = reader[i] == DBNull.Value ? entity.InfectionId : reader.GetInt64(i);
                        break;
                    case "stone_remain":
                        entity.StoneRemain = reader[i] == DBNull.Value ? entity.StoneRemain : reader.GetString(i);
                        break;
                    case "comment":
                        entity.Comment = reader[i] == DBNull.Value ? entity.Comment : reader.GetString(i);
                        break;
                    case "time_stamp":
                        entity.TimeStamp = reader.GetDateTime(i);
                        break;
                }
            }

            entity.EntityState = Stone.Components.EntityState.Saved;
            return entity;
        }

        #region 实体关联操作数据提供类

        #endregion
    }
}
